/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.LotteryDao;
import com.boxin.ims.modules.wechat.entity.Lottery;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 抽奖Service
 * @author Jakemanse
 * @version 2013-09-23
 */
@Component
@Transactional(readOnly = true)
public class LotteryService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(LotteryService.class);
	
	@Autowired
	private LotteryDao lotteryDao;
	
	public Lottery get(Long id) {
		return lotteryDao.findOne(id);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-1  上午1:37:12
	 * @function <p>获取店家的在线抽奖奖品列表  </p>
	 * @param userId
	 * @return
	 */
	public List<Lottery> findSellerLotteryListByUserId(Long userId){
		DetachedCriteria dc = lotteryDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		dc.add(Restrictions.eq("type", Lottery.LOTTERY_TYPE_SELLER));
		dc.add(Restrictions.eq("delFlag", Lottery.DEL_FLAG_NORMAL));
		return lotteryDao.find(dc);
	}
	
	public Page<Lottery> find(Page<Lottery> page, Lottery lottery) {
		DetachedCriteria dc = lotteryDao.createDetachedCriteria();
		if (lottery.getUser()!=null && lottery.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", lottery.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(lottery.getType())){
			dc.add(Restrictions.like("type", "%"+lottery.getType()+"%"));
		}
		if (StringUtils.isNotEmpty(lottery.getName())){
			dc.add(Restrictions.like("name", "%"+lottery.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(lottery.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+lottery.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", Lottery.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return lotteryDao.find(page, dc);
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-23  下午2:32:00
	 * @function <p> 根据UserId 获得 </p>
	 * @param userId
	 * @return
	 */
	public Lottery getByUserId(Long userId){
		
		DetachedCriteria dc = lotteryDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		
		List<Lottery> result = lotteryDao.find( dc);
		return (result == null || result.isEmpty()) ? null : result.get(0);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-30  下午12:08:15
	 * @function <p> 用户主动抽奖 设置 </p>
	 * @param userId
	 * @return
	 */
	public List<Lottery> getUserLotteryByUserId(Long userId){
		DetachedCriteria dc = lotteryDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		dc.add(Restrictions.eq("type", Lottery.LOTTERY_TYPE_USER));
		dc.add(Restrictions.eq("delFlag", Lottery.DEL_FLAG_NORMAL));
		
		List<Lottery> result = lotteryDao.find( dc);
		return result;
	}
	
	@Transactional(readOnly = false)
	public void save(Lottery lottery) {
		lotteryDao.save(lottery);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		lotteryDao.deleteById(id);
	}
	
}
